#!/usr/bin/env python
import xmlrpclib
import sys
import commands
import os

ip = '78.47.174.44' #<YOUR IP HERE

# create connection
server = xmlrpclib.ServerProxy("http://%s:8888"%ip)

maindir = os.path.join(os.path.dirname(os.path.abspath(__file__)))
activeplugindir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'plugins-active')

for file in os.listdir(activeplugindir):
  if not os.path.isfile(os.path.join(activeplugindir, file)) or file == 'plugin.sh':
    continue

  sys.stdout.write('%s ... '%file)
  output = commands.getstatusoutput('MUNIN_LIBDIR=%s %s'%(maindir, os.path.join(activeplugindir, file)))
  if output[0] != 0:
    print "local error: " + output[1]
    continue
  res = None
  try:
    res = server.update(file, output[1])
  except Exception, e:
    print e
    continue
  if type(res) != str:
   print res
  else:
    if res.strip() == 'config':
      output = commands.getstatusoutput('MUNIN_LIBDIR=%s %s config'%(maindir, os.path.join(activeplugindir, file)))
      if output[0] != 0:
        print "local error: " + output[1]
        continue
      try:
        print server.config(file, output[1])
      except Exception, e:
        print e
	continue
    else:
      print res
